/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generator;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.db.generator.field.models.DataBaseColumnType;

/**
 *
 * @author abosancic
 */
public class DatabaseUtils {

    public String[] getAllSchemas(Connection conn) {

        ArrayList<String> allSchemas = new ArrayList<>();

        try {
            try {
                DatabaseMetaData meta = conn.getMetaData();

                try (ResultSet schemas = meta.getCatalogs()) {  // for mysql
                    while (schemas.next()) {
                        String tableCatalog = schemas.getString(1);
                        //System.out.println("catalog: " + tableCatalog);
                        allSchemas.add(tableCatalog);
                    }
                }
            } catch (SQLException ex) {
                //Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//                JOptionPane.showMessageDialog(rootPane,"Your database connection is not set\nPlease check connection!","Connection info", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (Exception ex) {
            //Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//            JOptionPane.showMessageDialog(rootPane,"Your database connection is not set\nPlease check connection!","Connection info", JOptionPane.INFORMATION_MESSAGE);
        }

        return allSchemas.toArray(new String[allSchemas.size()]);
    }

    public String[] getAllTables(Connection conn, String schema) {

        ArrayList<String> allTables = new ArrayList<String>();

        try {
            DatabaseMetaData meta = conn.getMetaData();
            try (ResultSet schemas = meta.getTables(schema, null, "%", new String[]{"TABLE"})) {
                while (schemas.next()) {
                    String tableCatalog = schemas.getString("TABLE_NAME");
                    System.out.println("tableCatalog: " + tableCatalog);
                    allTables.add(tableCatalog);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

        return allTables.toArray(new String[allTables.size()]);
    }

    public String[] getAllColumns(Connection conn, String schema, String tabel) {

        ArrayList<DataBaseColumnType> allColumns = new ArrayList<>();

        try {
            ResultSet rsColumns = null;
            DatabaseMetaData meta = conn.getMetaData();
            rsColumns = meta.getColumns(schema, null, tabel, null);
            
            while (rsColumns.next()) {
                String columnName = rsColumns.getString("COLUMN_NAME");
                //System.out.println("column name=" + columnName);
                //String columnType = rsColumns.getString("TYPE_NAME");
                //System.out.println("type:" + columnType);
                String dataType = rsColumns.getString("DATA_TYPE");
                //System.out.println("data type:" + dataType);
                //String columnType1 = SQLTypeMap.convert(new Integer(dataType));
                //System.out.println("java data type:" + columnType1);
                int size = rsColumns.getInt("COLUMN_SIZE");
                //System.out.println("size:" + size);
                DataBaseColumnType ct = new DataBaseColumnType(columnName, dataType, size);
                //int nullable = rsColumns.getInt("NULLABLE");
                //if (nullable == DatabaseMetaData.columnNullable) {
                //    System.out.println("nullable true");
                //} else {
                //    System.out.println("nullable false");
                //}
                //int position = rsColumns.getInt("ORDINAL_POSITION");
                //System.out.println("position:" + position);
                allColumns.add(ct);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

        ArrayList<String> tmp = new ArrayList<>();
        for (DataBaseColumnType type : allColumns) {
            tmp.add(type.toString());
        }

        return tmp.toArray(new String[tmp.size()]);
    }
    
    public ArrayList<String> getPrimaryKeys(Connection conn, String shema, String table){
        ArrayList<String> list = new ArrayList<String>();

        try {
              
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet rs = meta.getPrimaryKeys(shema, null, table);
 
            while (rs.next()) {
                String columnName = rs.getString("COLUMN_NAME");
                System.out.println("getPrimaryKeys(): columnName=" + columnName);
                list.add(columnName);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
    
    public String [] getPrimaryKeyName(Connection conn, String shema, String table){
        ArrayList<String> pkname = new ArrayList<String>();
        try {
              
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet rs = meta.getPrimaryKeys(shema, null, table);
 
            while (rs.next()) {
                String columnName = rs.getString("COLUMN_NAME");
                System.out.println("getPrimaryKeys(): columnName=" + columnName);
                pkname.add(columnName);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (String [])pkname.toArray(new String[pkname.size()]);
    }
}
